package com.example.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Result;
import com.example.entity.Salesperson;
import com.example.service.SalespersonService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lmy
 * @since 2024-09-07
 */

@RestController
@RequestMapping("/salesperson")
@Api(description="客户经理")
public class SalespersonController {
    @Autowired
    private SalespersonService salespersonService;


    @GetMapping("/findAll")
    @ApiOperation(value = "查询所有客户经理")
    public Result list() {
        QueryWrapper<Salesperson> queryWrapper = new QueryWrapper<>();
        List<Salesperson> list = salespersonService.list(queryWrapper);
        return Result.ok().data("items", list);
    }
    @PostMapping("/addkhjl")
    @ApiOperation(value = "添加客户经理")
    public Result add_khjl(@RequestBody Salesperson salesperson) {

        boolean id =salespersonService.save(salesperson);

            if(id){
                return Result.ok();
            }else{
                return Result.error();

            }

        }

    @PostMapping("/deletekhjl/{id}")
    @ApiOperation(value = "删除客户经理")
    public Result delete_khjl(@PathVariable long id) {
        boolean b = salespersonService.removeById(id);
        if(b){
            return Result.ok();
        }else{
            return Result.error();

        }
    }
    @PostMapping("/editekhjl")
    @ApiOperation(value = "修改客户经理")
    public Result edite_khjl( @RequestBody Salesperson salesperson) {
        boolean b = salespersonService.updateById(salesperson);
        if(b){
            return Result.ok();
        }else{
            return Result.error();

        }
    }

    @ApiOperation(value = "分页客户经理列表")
    @GetMapping("/pageList/{page}/{limit}")
    public  Result pageList(@ApiParam(name = "page", value = "当前页码", required = true)@PathVariable Long page,
                       @ApiParam(name = "limit", value = "每页记录数", required = true)@PathVariable Long limit){
        Page<Salesperson> pageParam=new Page<>(page,limit);
        salespersonService.page(pageParam,null);
        List<Salesperson > records=pageParam.getRecords();//获取当前页的记录列表
        Long total=pageParam.getTotal();
        return Result.ok().data("total",total).data("rows",records);
    }
    @ApiModelProperty(value = "根据Id查询")
    @GetMapping("/getById/{id}")
    public  Result getById(@PathVariable Long id){
        Salesperson salesperson=salespersonService.getById(id);
        return  Result.ok().data("salesperson",salesperson);
    }
    @ApiModelProperty(value = "根据salespersonId查询")
    @GetMapping("/getBysalespersonId/{salespersonId}")
    public  Result getByBysalespersonId(@PathVariable String salespersonId){
        Salesperson salesperson = salespersonService.findSalespersonById(salespersonId);
        return  Result.ok().data("salesperson",salesperson);
    }





}
